package cn.com.bluemoon.daps.model.core.file;

import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.common.toolkit.BmAssetUtils;
import cn.com.bluemoon.daps.model.entity.DapDataModel;
import cn.com.bluemoon.daps.model.entity.DapDataModelTableField;
import lombok.Getter;
import lombok.Setter;

import java.util.Optional;

/**
 * 字段抽象类
 *
 * @author Jarod.Kong
 * @date 2021/2/7 16:17
 */
public abstract class FieldData extends IMetaData<DapDataModelTableField>
        implements IConvertToModel<DapDataModelTableField> {

    /**
     * 增加排序
     */
    @Setter
    @Getter
    private Integer fieldRank;

    @Override
    public ResultBean<DapDataModelTableField> convertToModel(DapDataModel model) {
        BmAssetUtils.notNull(model, "定义好模型");

        DapDataModelTableField field = new DapDataModelTableField();
        field.setId(null);
        field.setDefaultValue(getDefaultVal());
        field.setAutoIncrement(isOne(getAutoIncrement()));
        field.setBusinessDesc(getFieldBizDesc());
        field.setComment(getFieldNameZh());
        field.setDataModelId(model.getId());
        field.setDataType(getFiledDataType());
        // 存在复合字段（map/structs/array）情况，eg: array<struct<pre_sale:string>> 对应的dataType:array,
        // compoundType:<struct<pre_sale:string>>
        field.setCompoundType(getCompoundType());
        field.setFieldName(getFieldName());
        field.setLength(getFieldLength());
        field.setDecimalLength(getIntOrNull(getDecimalLength()));
        field.setNotNull(isOne(getNotNull()));
        field.setPrimaryKey(isOne(getPrimaryKey()));
        field.setFieldRank(Optional.ofNullable(getFieldRank()).orElse(getOrder()));
        field.setUnSigned(isOne(getUnSigned()));
        field.setZeroFill(isOne(getFillZero()));
        field.setAutoIncrDefaultValue(getLongOrNull(getInitValue()));
        field.setPId("0");
        field.setDataNewest(1);
        field.setStandardMethod(null);
        field.setModelTableId(null);
        return ResultBean.ok(field);
    }

    protected Integer getIntOrNull(String str) {
        if (str == null) return null;
        try {
            return Integer.valueOf(str);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return null;
        }
    }

    protected Long getLongOrNull(String str) {
        if (str == null) return null;
        try {
            return Long.valueOf(str);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return null;
        }
    }

    protected <T extends Number> String getStrOrNull(T decimalLength) {
        return decimalLength == null ? null : String.valueOf(decimalLength);
    }

    @Override
    public String sheetName() {
        return "字段";
    }

    @Override
    public Integer sheetNo() {
        return 1;
    }

    /**
     * 来自数据库的模型
     *
     * @param model 来自数据库的模型
     */
    @Override
    public FieldData copyDbProperties0(DapDataModelTableField model) {
        return this;
    }

    public abstract String getFieldName();

    public abstract String getFiledDataType();

    public abstract String getCompoundType();

    public abstract String getFieldLength();

    public abstract Boolean getPrimaryKey();

    public abstract Boolean getNotNull();

    public abstract String getDefaultVal();

    public abstract String getFieldNameZh();

    public abstract String getFieldBizDesc();

    protected abstract Boolean getFillZero();

    protected abstract Boolean getUnSigned();

    protected abstract Boolean getAutoIncrement();

    protected abstract String getInitValue();

    protected abstract String getDecimalLength();

}

